load datain/agents.csv

for a=1:size(agents,1)

P_file=['PQ/' int2str(agents(a,1)) '.p'];
[P_hdr,P]=hdrload(P_file);
Q_file=['PQ/' int2str(agents(a,1)) '.q'];
[Q_hdr,Q]=hdrload(Q_file);

% P=[1,2,.5;1,.5,2];
% Q=[0,2,0;1,0,2];

% P=[1,2,.5;1,.5,2];
% Q=[0,0,0;1,2,.5];

% Check for cycles in the RP
PQ=P'*Q;
EXP=diag(PQ)*ones(length(PQ),1)';
V=EXP-PQ>0;
E=EXP-PQ;

% Add elements to diagonal
V1=max(V,eye(length(V),length(V)));

data=dataprepnew(V1,E);
dlmwrite(['datain/R_' int2str(agents(a,1)) '.csv'],data);

total_budgets(a,1)=sum(sum(P.*Q));

Cs=V.*E;
Cs=Cs./EXP;
Es=Cs;
for k=1:length(Cs)
    for j=1:length(Cs)
        for i=1:length(Cs)
            if and(Es(i,k)>0,Es(k,j)>0)
                if Es(i,j)==0
                    Es(i,j)=min([Es(i,k),Es(k,j)]);
                else
                    Es(i,j)=max([min([Es(i,k),Es(k,j)]),Es(i,j)]);
                end
            end
        end
    end
end

afriat(a,1)=1-max(diag(Es));

end % for a=1:size(agents,1)

xlswrite('afriat.xls', afriat)
xlswrite('total_budgets.xls', total_budgets)
